home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / AmigaE / Src / Afc / Worldbuilder_Example1.e < prev    next >
Encoding:
Text File  |  1997-08-02  |  7.4 KB  |  225 lines

  1. /*
  2.   worldbuilder example.
  3.  
  4.   Needs: 'bitmap_obj' AND 'explain_exception' from AFC.
  5.  
  6.   Instructions:   left - right arrow: move observer x
  7.                   up - down    arrow: move observer y
  8.                   Del - Help        : move observer z
  9.                   q - w             : rotate around x
  10.                   a - s             : rotate around y
  11.                   z - x             : rotate around z
  12.                   space             : reset TO defaults
  13.                   Esc               : quit
  14. */
  15.  
  16. MODULE 'AFC/worldbuilder',
  17.        'AFC/bitmapper',
  18.        'AFC/explain_exception',
  19.        'intuition/intuition',
  20.        'intuition/screens',
  21.        'graphics/view',
  22.        'exec/ports'
  23.  
  24.  
  25. PROC main() HANDLE
  26.   DEF scene:PTR TO worldbuilder
  27.   DEF scr=NIL:PTR TO screen, rp, vp:PTR TO viewport
  28.   DEF win=NIL:PTR TO window, port:PTR TO mp
  29.   DEF msg:PTR TO intuimessage, class, code
  30.   DEF oox,ooy,ooz, rrx,rry,rrz
  31.   DEF bm2:PTR TO bitmapper, rp2, bitmap2, ri:PTR TO rasinfo
  32.   DEF usebm=0, scrbm
  33.  
  34.   -> fillSinTable() IS done automatically by the following:
  35.   NEW scene.worldbuilder()
  36.  
  37.   -> we add three objects
  38.   scene.addobj('cube')
  39.   scene.addobj('axis')
  40.   scene.addobj('pyramid')
  41.  
  42.   -> vertices, lines AND surfaces FOR the 'cube' object_obj:
  43.   ->                 name    x1,y1, z1, x2,y2, z2, ...
  44.   scene.fastsetvert('cube',[-50,50,-50, 50,50,-50, 50,-50,-50, -50,-50,-50,
  45.                 -50,50,50, 50,50,50, 50,-50,50, -50,-50,50]:vertex_obj)
  46.   ->                                 MUST be a typed LIST:  ^^^^^^^^^^
  47.  
  48.   ->                 name   startv1, endv1, colour1, ...
  49.   scene.fastsetline('cube',[0,        1,       2,    1,2,2, 2,3,2, 3,0,2,
  50.                             4,5,2, 5,6,2, 6,7,2, 7,4,2,
  51.                             0,4,2, 1,5,2, 2,6,2, 3,7,2]:line_obj)
  52.  
  53.   ->                name,num,vertices,colour
  54.   scene.setsurface('cube',0,[0,1,2,3],2)
  55.   scene.setsurface('cube',1,[0,4,5,1],3)
  56.   scene.setsurface('cube',2,[7,6,5,4],4)
  57.   scene.setsurface('cube',3,[3,2,6,7],5)
  58.   scene.setsurface('cube',4,[0,3,7,4],6)
  59.   scene.setsurface('cube',5,[1,5,6,2],7)
  60.  
  61.   -> definition OF the three axes
  62.   scene.fastsetvert('axis',[0,0,0, 100,0,0, 0,100,0, 0,0,100]:vertex_obj)
  63.  
  64.   scene.fastsetline('axis',[0,1,3, 0,2,4, 0,3,5]:line_obj)
  65.  
  66.   -> AND now there comes the 'pyramid' object_obj
  67.   scene.fastsetvert('pyramid',[0,60,-50, 50,60,-50, 50,60,50,
  68.                                0,60,50, 25,120,0]:vertex_obj)
  69.  
  70.   scene.fastsetline('pyramid',[0,1,6, 1,2,6, 2,3,6, 3,0,6,
  71.                                0,4,6, 1,4,6, 2,4,6, 3,4,6]:line_obj)
  72.  
  73.   scene.setsurface('pyramid',0,[0,1,2,3],3)
  74.   scene.setsurface('pyramid',1,[0,4,1],2)
  75.   scene.setsurface('pyramid',2,[3,4,0],4)
  76.   scene.setsurface('pyramid',3,[2,4,3],5)
  77.   scene.setsurface('pyramid',4,[1,4,2],6)
  78.  
  79.  
  80.   scene.setdisplay(320,256) -> display dimensions
  81.   scene.init3D() -> build system structures FOR filled vectors
  82.  
  83.   scene.setaftertrasl(160,128) -> origin OF the screen axes.
  84.   scene.setafterscale(200,200) -> scaling AFTER projection (TO fit in
  85.                                -> the screen): 200 = double.
  86.   scene.setobserver(0,0,-200)  -> position OF the observer
  87.  
  88.   -> we open a screen
  89.   scr:=OpenScreenTagList(NIL,[SA_TOP,0,
  90.                               SA_LEFT,0,
  91.                               SA_WIDTH,320,
  92.                               SA_HEIGHT,256,
  93.                               SA_DEPTH,3,
  94.                               SA_TITLE,'Routines3D',
  95.                               SA_SHOWTITLE,FALSE,
  96.                               0,0])
  97.   IF scr=NIL THEN Raise("SCR")
  98.   vp:=scr.viewport
  99.   ri:=vp.rasinfo    -> we get the rasinfo structure FOR double buffering
  100.   scrbm:=ri.bitmap
  101.  
  102.   -> a trivial palette
  103.   SetRGB4(vp,3,15,0,0)
  104.   SetRGB4(vp,4,0,15,0)
  105.   SetRGB4(vp,5,0,0,15)
  106.   SetRGB4(vp,7,12,12,0)
  107.  
  108.   -> we allocate a second bitmap FOR the double buffering
  109.   NEW bm2.bitmapper()
  110.   bm2.allocbitmap(320,256,3,TRUE)
  111.   rp2:=bm2.rastport()
  112.   bitmap2:=bm2.bitmap()
  113.  
  114.   -> the window IS needed TO get keypresses
  115.   win:=OpenWindowTagList(NIL,[WA_TOP,0,
  116.                               WA_LEFT,0,
  117.                               WA_WIDTH,320,
  118.                               WA_HEIGHT,256,
  119.                               WA_TITLE,'Routines3D',
  120.                               WA_FLAGS,WFLG_ACTIVATE OR WFLG_SMART_REFRESH OR WFLG_BACKDROP OR WFLG_BORDERLESS,
  121.                               WA_IDCMP,IDCMP_RAWKEY,
  122.                               WA_CUSTOMSCREEN,scr,
  123.                               0,0])
  124.   IF win=NIL THEN Raise("WIN")
  125.   rp:=win.rport
  126.   port:=win.userport
  127.  
  128.   oox:=0 ; ooy:=0 ; ooz:=-200  -> observer initial position
  129.   rrx:=0 ; rry:=0 ; rrz:=0  -> angles OF rotation
  130.  
  131.   scene.projection()  -> project
  132.   scene.drawfill(rp)  -> AND draw the scene
  133.   usebm:=1-usebm
  134.  
  135.   LOOP
  136.     Wait(Shl(1,port.sigbit))
  137.     REPEAT
  138.       IF (msg:=GetMsg(port))<>NIL
  139.         class:=msg.class
  140.         code:=msg.code
  141.         ReplyMsg(msg)
  142.         SELECT class
  143.           CASE IDCMP_RAWKEY  -> IF user presses a key...
  144.             SELECT code
  145.               CASE $4C -> up arrow
  146.                 ooy:=ooy+1
  147.                 scene.setobserver(oox,ooy,ooz)  -> change observer position
  148.               CASE $4D -> down arrow
  149.                 ooy:=ooy-1
  150.                 scene.setobserver(oox,ooy,ooz)
  151.               CASE $4E -> right arrow
  152.                 oox:=oox+1
  153.                 scene.setobserver(oox,ooy,ooz)
  154.               CASE $4F -> left arrow
  155.                 oox:=oox-1
  156.                 scene.setobserver(oox,ooy,ooz)
  157.               CASE $46 -> del
  158.                 ooz:=ooz-1
  159.                 scene.setobserver(oox,ooy,ooz)
  160.               CASE $5F -> help
  161.                 ooz:=ooz+1
  162.                 scene.setobserver(oox,ooy,ooz)
  163.               CASE $10 -> q
  164.                 rrx:=rrx-1
  165.                 scene.setrot(rrx,rry,rrz)  -> SET NEW rotation angles
  166.               CASE $11 -> w
  167.                 rrx:=rrx+1
  168.                 scene.setrot(rrx,rry,rrz)
  169.               CASE $20 -> a
  170.                 rry:=rry-1
  171.                 scene.setrot(rrx,rry,rrz)
  172.               CASE $21 -> s
  173.                 rry:=rry+1
  174.                 scene.setrot(rrx,rry,rrz)
  175.               CASE $31 -> z
  176.                 rrz:=rrz-1
  177.                 scene.setrot(rrx,rry,rrz)
  178.               CASE $32 -> x
  179.                 rrz:=rrz+1
  180.                 scene.setrot(rrx,rry,rrz)
  181.               CASE $40 -> space
  182.                 oox:=0 ; ooy:=0 ; ooz:=-200
  183.                 rrx:=0 ; rry:=0 ; rrz:=0
  184.                 scene.setobserver(oox,ooy,ooz)
  185.                 scene.setrot(rrx,rry,rrz)
  186.               CASE $45 -> esc
  187.                 REPEAT
  188.                   IF (msg:=GetMsg(port))<>NIL THEN ReplyMsg(msg)
  189.                 UNTIL msg=NIL
  190.                 Raise("end") -> quit
  191.             ENDSELECT
  192.         ENDSELECT
  193.       ENDIF
  194.     UNTIL msg=NIL
  195.     scene.projection()  -> project the scene
  196.  
  197.     SetAPen(IF usebm=0 THEN rp ELSE rp2, 0)
  198.     RectFill(IF usebm=0 THEN rp ELSE rp2, 0,0,320,256) -> clear the screen
  199.  
  200.     scene.drawfill(IF usebm=0 THEN rp ELSE rp2) -> eventually draw
  201.     ->    ^^^^^^^^: change this in 'draw' IF you want TO see wireframe objects
  202.  
  203.     IF usebm=0          -> handmade double buffering (in a screen)
  204.       ri.bitmap:=scrbm
  205.     ELSE                    -> swap the two bitmaps
  206.       ri.bitmap:=bitmap2
  207.     ENDIF
  208.     WaitTOF()
  209.     ScrollVPort(vp)         -> update copperlist TO NEW pointers
  210.     usebm:=1-usebm
  211.   ENDLOOP
  212.  
  213. EXCEPT DO
  214.   IF win THEN CloseWindow(win)
  215.   IF scr
  216.     ri.bitmap:=scrbm
  217.     CloseScreen(scr)
  218.   ENDIF
  219.   END scene  -> remember TO free ALL memory
  220.   END bm2
  221.   IF exception<>"end" THEN explain_exception()
  222.   CleanUp(0)
  223. ENDPROC
  224.  
  225.